package ch.zbw.sysventory.server.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.zbw.sysventory.server.model.ScanConfiguration;
import ch.zbw.sysventory.server.repository.ScanConfigurationRepository;
import ch.zbw.sysventory.server.service.ScanConfigurationService;

@Transactional
@Service
public class ScanConfigurationServiceImpl implements ScanConfigurationService {

	@Autowired(required = true)
	private ScanConfigurationRepository scanConfigurationRepository;

	@Override
	public ScanConfiguration getConfiguration() {
		ScanConfiguration configuration;
		List<ScanConfiguration> configurations = this.scanConfigurationRepository.findAll();
		if (configurations.size() > 0) {
			configuration = configurations.get(0);
		} else {
			configuration = new ScanConfiguration();
			configuration.setAgentPort(22000);
			this.scanConfigurationRepository.save(configuration);
		}
		return configuration;
	}

	@Override
	public ScanConfiguration update(ScanConfiguration scanConfiguration) throws Exception {
		return this.scanConfigurationRepository.saveAndFlush(scanConfiguration);
	}

}
